package org.boulderfoodrescue.webapp.client.old.viewer.savepickup;

import org.boulderfoodrescue.webapp.client.googlemap.GoogleMap;
import org.boulderfoodrescue.webapp.client.old.datamanager.DataManager;
import org.boulderfoodrescue.webapp.client.old.viewer.SideBarPanel;
import org.boulderfoodrescue.webapp.client.old.viewer.scheduler.ShortDateFormat;
import org.boulderfoodrescue.webapp.shared.LoginInfo;
import org.boulderfoodrescue.webapp.shared.Place;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.maps.client.geocode.Placemark;
import com.google.gwt.regexp.shared.RegExp;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;

public class SavePickupPanel extends Composite{

  private static SaveAddressPanelUiBinder uiBinder = GWT
      .create(SaveAddressPanelUiBinder.class);

  interface SaveAddressPanelUiBinder extends
  UiBinder<Widget, SavePickupPanel> {
  }

  public SavePickupPanel(SideBarPanel parent, GoogleMap gMap) {
    this.parent = parent;
    this.gMap = gMap;
    initWidget(uiBinder.createAndBindUi(this));
    date_box.setFormat(new ShortDateFormat());
  }
  @UiField
  TextBox user_name;
  @UiField
  TextBox user_email;
  @UiField
  TextBox user_phone;
  @UiField
  Label address_found_text1;
  @UiField
  Label address_found_text2;
  @UiField
  Button button_next;
  @UiField
  Button button_cancel_save;
  @UiField
  TextBox input_name;
  @UiField 
  DateBox date_box;
  @UiField
  ListBox ammount_food;
  @UiField
  CheckBox repeats;
  @UiField
  TextArea pickup_notes;
  protected Place current;
  protected GoogleMap gMap;
  protected SideBarPanel parent;
  private static final String TextBoxFormatError = "dateBoxFormatError";

  public void onAddressFound(Placemark location){
    //setup place location
    Place place = new Place();
    place.setAddressOne(location.getStreet());
    place.setCity(location.getCity());
    place.setState(location.getState());
    place.setZip_code(location.getPostalCode());
    address_found_text1.setText(place.topLineString());
    address_found_text2.setText(place.bottomLineString());
//    gMap.addMarker(place, location.getPoint(), true);
    place.setLatitude(location.getPoint().getLatitude());
    place.setLongitude(location.getPoint().getLongitude());
    place.setUsername(DataManager.getLoginInfo().getEmailAddress());
    current = place;
    //fill in text boxes with user info
    LoginInfo info = DataManager.getLoginInfo();
    if(info.getFirst_name() != null)
      user_name.setText(info.getFirst_name());
    if(info.getLast_name() != null)
      user_name.setText(user_name.getText() + " " + info.getLast_name());
    if(info.getEmailAddress() != null)
      user_email.setText(info.getEmailAddress());
    if(info.getPhone() != null)
      user_phone.setText(info.getPhone());
  }

  @UiHandler("button_next")
  public void onButtonSave(ClickEvent e){
    String contactName = user_name.getText();
    if(contactName.length() == 0){
    	user_name.addStyleName(TextBoxFormatError);
    	Window.alert("Your Name field cannot be empty");
    	return;
    } else
    	user_name.removeStyleName(TextBoxFormatError);
    String email = user_email.getText();
    RegExp regex_email = RegExp.compile("^[\\w-]+(\\.[\\w-]+)*@([a-z0-9-]" +
    		"+(\\.[a-z0-9-]+)*?\\.[a-z]{2,6}|(\\d{1,3}\\.){3}\\d{1,3})(:\\d{4" +
    		"})?$");
    if(!regex_email.test(email)){
    	user_email.addStyleName(TextBoxFormatError);
    	Window.alert("Email field must contain a valid email");
    	return;
    } else
    	user_email.removeStyleName(TextBoxFormatError);
    String phone = user_phone.getText();
    RegExp regex_phone = RegExp.compile("^(?:(?:\\+?1\\s*(?:[.-]\\s*)?)?(?:\\(\\s" +
    		"*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\\s*\\)|([2-9]1[02-9" +
    		"]|[2-9][02-8]1|[2-9][02-8][02-9]))\\s*(?:[.-]\\s*)?)?([2-9]1[02-9]" +
    		"|[2-9][02-9]1|[2-9][02-9]{2})\\s*(?:[.-]\\s*)?([0-9]{4})(?:\\s*(?:" +
    		"#|x\\.?|ext\\.?|extension)\\s*(\\d+))?$");
    if(!regex_phone.test(phone)){
    	user_phone.addStyleName(TextBoxFormatError);
    	Window.alert("Phone field must contain a valid phone number");
    	return;
    } else
    	user_phone.removeStyleName(TextBoxFormatError);
    String name = input_name.getText();
    if(name.length() == 0){
      input_name.addStyleName(TextBoxFormatError);
      Window.alert("Location Name field cannot be empty");
    	return;
    } else
    	input_name.removeStyleName(TextBoxFormatError);
    long a_level = Long.parseLong(ammount_food.getValue(ammount_food.getSelectedIndex()));
    current.setName(name);
    if(date_box.getValue() == null){
      Window.alert("The expiration date must be a valid date");
      return;
    }
    
    current.setExpiration_date(date_box.getValue().getTime());
    current.setAmount(a_level);
//    current.setLatitude(gMap.getMarker(current).getLatLng().getLatitude());
//    current.setLongitude(gMap.getMarker(current).getLatLng().getLongitude());
    current.setContact_name(contactName);
    current.setEmail(email);
    current.setPhone(phone);
    current.setNotes(pickup_notes.getText());
    
    parent.addressInfoEntered(current);
  }

  @UiHandler("button_cancel_save")
  public void onButtonCancelSave(ClickEvent e){
//    gMap.removeMarker(current);
    parent.onCancelNewPickup();
  }
  
  @UiHandler("repeats")
  public void onRepeatsClick(ClickEvent event){
  	boolean checked = ((CheckBox) event.getSource()).getValue();
  	current.setRepeats(checked);
  }
  
}
